﻿#include "GlobalVars.h"
#include "Memory.h"
#include "Renderer.h"
#include "Player.h"
#include <iostream>

void GlobalVars::updatePlayerList()
{
	GlobalVars::get().uLevelAddr = Memory::get().read<uintptr_t>(GlobalVars::get().worldAddr + GlobalVars::get().ofs.level_offset);
	GlobalVars::get().actorCount = Memory::get().read<int>(GlobalVars::get().uLevelAddr + GlobalVars::get().ofs.count_offset);
	GlobalVars::get().actorsAddr = Memory::get().read<uintptr_t>(GlobalVars::get().uLevelAddr + GlobalVars::get().ofs.actors_offset);

	//cout << "uLevelAddr = " << GlobalVars::get().uLevelAddr << endl;
	//cout << "actorCount = " << GlobalVars::get().actorCount << endl;
	//cout << "actorsAddr = " << GlobalVars::get().actorsAddr << endl;

	activeEnemyCounter = 0;

	for (int i = 0; i < GlobalVars::get().actorCount; i++)
	{
		uintptr_t actorBaseAddr = Memory::get().read<uintptr_t>(GlobalVars::get().actorsAddr + i * 0x8);
		shared_ptr<Player> player = make_shared<Player>(actorBaseAddr);
		player->update();
		if (player->hp < 1)
		{
			continue;
		}

		if (player->type == PlayerType::enemy)
		{
			enemyList[activeEnemyCounter] = player;
			activeEnemyCounter++;
		}
		else if (player->type == PlayerType::oneself)
		{
			localPlayer = player;
		}
	}
}

void GlobalVars::printOffsets()
{
	Offsets * base = &ofs;
	cout << base << " = link to struct of offsets { \n\n";
	for (int i = 0; i < sizeof(Offsets) / sizeof(DWORD); i++)
	{
		cout << hex << "0x" << *(uintptr_t *)((uintptr_t)base + i * sizeof(DWORD)) << dec << "\n";
	}
	cout << "} \n";
}
